﻿@model ScrumProject.ViewModels.ProjectIndexData
@{
    ViewBag.Title = "Backlog";
    ScrumProject.Models.StoryRepository sr = new ScrumProject.Models.StoryRepository();
    ScrumProject.Models.RealizedWorkRepository rwr = new ScrumProject.Models.RealizedWorkRepository();
}
@if (ViewBag.nosprint)
{
<script type="text/javascript">window.alert("There is no active sprints");</script>
}
<h2>Product Backlog</h2>
<div id="user-nav" class="navbar">
    <ul class="nav btn-group">
        <li class="btn" onclick="unfinishedStories()">Unfinished Stories
        </li>
        <li class="btn" onclick="finishedStories()">Finished Stories
        </li>
    </ul>
</div>
<script type="text/javascript">
    function unfinishedStories() {
        document.getElementById("stories").innerHTML=document.getElementById("unfinished-stories").innerHTML;
    }
    function finishedStories() {
        document.getElementById("stories").innerHTML = document.getElementById("finished-stories").innerHTML;
    }
    function checkboxes() {
        var checkboxs;
        var timecompl = document.getElementsByName("item.TimeComplexity");
        checkboxs = [];
        checkboxs = document.getElementsByName("storyNumber");
        for (var i = 0; i < checkboxs.length; i += 1) {
            if (timecompl[i].value == 0) {
                checkboxs[i].style.visibility = "hidden";
            }
            else {
                checkboxs[i].style.visibility = "inherit";
            }
        }
    }
</script>
<script type="text/javascript">
    function TimeComplexityItem(id, itemComplexity) {
        this.itemComplexity = itemComplexity;
        this.id = id;
        

    }
    $(document).ready(function () {
        $("#timeComplexitySave").click(function () {
            var ListComp = new Array();
            var listComplexities = new Array();
            var listItems = new Array();
            var neg = 0;
            $("input").each(function () {

                if ($(this).attr("id") == "itemComplexity")
                { if ($(this).val() < 0) { neg = 1; } listComplexities.push($(this).val()); }
                if ($(this).attr("id") == "itemId")
                { listItems.push($(this).val()); }

            });
            if (neg != 1) {
                for (var i = 0; i <= listComplexities.length; i++) {
                    var exists = 0;
                    for (var j = 0; j <= ListComp.length; j++) {

                        if (listItems[i] == listItems[j] && i != j) {
                            exists = 1;
                        }
                        if (listComplexities[i] == undefined && listItems[i] == undefined) {
                            exists = 1;
                        }
                    }
                    if (exists == 0) {

                        var tmp = new TimeComplexityItem;
                        tmp.id = listItems[i];
                        tmp.itemComplexity = listComplexities[i];
                        ListComp.push(tmp);
                    }
                }

                var things = JSON.stringify({ 'ListComp': ListComp });
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: '/smrpo6/MyProjects/SaveTimeComplexity',
                    contentType: 'application/json; charset=utf-8',
                    data: things




                });
                checkboxes();
            } else {
                alert("negative values are not alowed")
            }
        });

        checkboxes();



    });

</script>

<center><div id="stories" style="width : 700px">
</div></center>

@if (Model.Stories != null)
{
    
    <div id="finished-stories" style="height : 0px; visibility : hidden">
        <h3>Finished User Stories</h3>    
        @foreach (var item in Model.Stories)
            {
                if (item.Status.CompareTo("finished  ") != 0) { continue; }
                string[] testi = item.Tests.Split('\n');
                string hash = "# ";
                <table class='table table-condensed'  style="border: 1px solid #CFCACC;">
                    <thead>
                        <tr>
                            <th style="text-align: left; background-image: -moz-linear-gradient(center top , rgb(203, 203, 203) 0%, rgb(234, 234, 234) 100%);">
                                <h5><b>@item.Name</b></h5>
                                Priority: @item.Priority have&nbsp;|&nbsp;Business value: @item.BusinessValue&nbsp;|&nbsp;Actions: @Html.ActionLink("Notes", "Notes", "Story", new { id = item.Id }, null)
                            </th>
                            <th style="text-align: right; background-image: -moz-linear-gradient(center top , rgb(203, 203, 203) 0%, rgb(234, 234, 234) 100%);">
                                Estimate: @item.TimeComplexity pt | Work done: @((int)(sr.WorkDone(item.Id)!=null?sr.WorkDone(item.Id):0)) 
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr><td colspan="2">@item.Description</td></tr>
                        <tr><td colspan="2" style="color:#0088cc;">
                                @foreach (string st in testi)
                                {
                                    @hash  @st; <br />
                                }
                            </td></tr>
                    </tbody>
                </table>
            } 
    </div>
    <div id="unfinished-stories" style="height : 0px; visibility : hidden">

        <h3>User Stories for current Sprint</h3>    
        
            @foreach (var item in Model.Stories)
            { 
                using (Html.BeginForm("Details","Story",item)){
            
                if (item.Status.CompareTo("active    ") != 0) { continue; }
                string[] testi = item.Tests.Split('\n');
                string hash = "# ";
                <table class='table table-condensed' style="border: 1px solid #CFCACC;">
                    <thead>
                        <tr>
                            <th style="text-align: left; background-image: -moz-linear-gradient(center top , rgb(203, 203, 203) 0%, rgb(234, 234, 234) 100%);">
                                <h5><b>@item.Name</b></h5>
                                Priority: @item.Priority have&nbsp;|&nbsp;Business value: @item.BusinessValue&nbsp;|&nbsp;Actions: @Html.ActionLink("Notes", "Notes", "Story", new { id = item.Id }, null)
                            </th>
                            <th style="text-align: right; background-image: -moz-linear-gradient(center top , rgb(203, 203, 203) 0%, rgb(234, 234, 234) 100%);">
                                Estimate: @item.TimeComplexity pt | Work done: @((int)(sr.WorkDone(item.Id)!=null?sr.WorkDone(item.Id):0)) 
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr><td colspan="2">@item.Description</td></tr>
                        <tr><td colspan="2" style="color:#0088cc;">
                                @foreach (string st in testi)
                                {
                                    @hash  @st; <br />
                                }
                            </td></tr>
                            <tr>
                                <td colspan="2" style="color:Orange;">
                                    @item.Notes
                                </td>
                            </tr>
                             <tr><td>Tasks (All/completed): @item.Tasks.Count / @item.Tasks.Count(tsk => tsk.Status ==3) |
                               Work (spent/remaining): @((int)(sr.WorkDone(item.Id)!=null?sr.WorkDone(item.Id):0)) h
                                / @{double? workr = sr.WorkReamining(item.Id); ;
                                    /*try { workr = (int)rwr.getRealizedWorksByTask(item.Id, DateTime.Today); }
                                  catch{ workr =-1;}*/
                                  @* != null ? rwr.getRealizedWorkByTask(item.Id, DateTime.Today).WorkRemaining : 0))*@ @workr} h</td>
                                <td>
                                @if (ViewBag.owner)
                                {
                                        @Html.HiddenFor(model => item.ProjectId, new { })
                                        if(item.Tasks.Where(tsk => tsk.Status != 3).Count() == 0){
                                       <input type="submit" name="submitButton" value="Accept" class="btn" />                                                                                    
                                    }
                                       <input type="submit" name="submitButton" value="Reject" class="btn" />  
                                }</td></tr>
                        @*<tr><td colspan="2">
                                <table class="table table-condensed">
                                    <thead><tr>
                                            <th width="40%" style="text-align: left;">Tasks</th>
                                            <th width="25%" style="text-align: left;">Status/Action</th>
                                            <th width="25" style="text-align: left;">Member</th>
                                            <th width="10%" style="text-align: left;">Remaining</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    @foreach (var task in item.Tasks)
                                    {
                                        <tr><td style="text-align: left;">@task.Description</td>
                                            <td style="text-align: left;">
                                                @if (task.Status.Equals(true))
                                                {
                                                    Write("Completed");
                                                }
                                                else
                                                {
                                                    Write("Assigned");
                                                }
                                            </td>
                                            <td style="text-align: left;">
                                                @ur.GetUser((int)task.UserId).Username
                                            </td>
                                            <td style="text-align: left;">@task.WorkRemaining</td>
                                        </tr>
                                    }
                                    </tbody>
                                </table>
                            </td>
                        </tr>*@
                    </tbody>
                </table>
                }
            } 


        <h3>List of Remaining User Stories</h3>
        @using (Html.BeginForm())
        {
            int i = 0;
            foreach (var item in Model.Stories)
            {
                if (item.Status.CompareTo("inactive  ") != 0) { continue; }
                //if (item.SprintId != null) { continue; }
                i++;

                string[] testi = item.Tests.Split('\n');
                string hash = "# ";
                <table class='table table-condensed'  style="border: 1px solid #CFCACC;">
                    <thead>
                        <tr>
                            <th style="text-align: left; background-image: -moz-linear-gradient(center top , rgb(203, 203, 203) 0%, rgb(234, 234, 234) 100%);">
                                <h5>                
                                @if (item.TimeComplexity != null && item.TimeComplexity > 0)
                                { }
                                    <input id="hidecheck" type='checkbox' name='storyNumber' value='@i'/>
                                <b>@item.Name</b></h5>
                                Priority: @item.Priority have&nbsp;|&nbsp;Business value: @item.BusinessValue&nbsp;|&nbsp;Actions: @Html.ActionLink("Notes", "Notes", "Story", new { id = item.Id }, null)
                            </th>
                            <th style="text-align: right; background-image: -moz-linear-gradient(center top , rgb(203, 203, 203) 0%, rgb(234, 234, 234) 100%);">
                                Estimate: @Html.TextBoxFor(model => item.TimeComplexity, new { style = "width: 20px", id = "itemComplexity" }) pt
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr><td colspan="2">@item.Description</td></tr>
                        <tr><td colspan="2" style="color:#0088cc;">
                                @foreach (string st in testi)
                                {
                                    @hash  @st; <br />
                                }
                            </td></tr>
                            <tr>
                                <td colspan="2" style="color:Orange;">
                                    @item.Notes
                                </td>
                            </tr>
                    </tbody>
                </table>
                @Html.HiddenFor(model => item.Id, new { id = "itemId" })
            }

            if (ViewBag.manageStories)
            { 
            <div style="position:absolute">
                <input type="submit" value="Add to Sprint" class="btn" />
                <input type="button" value="Save time complexities" id="timeComplexitySave" class="btn" />
            </div>
            <div style="right:auto">@Html.ActionLink("Manage Stories", "Index", "Story", new { projectId = ViewBag.projectId }, null)</div>
            }
        }
    </div>
}
   

     
     @if (Model.Sprints != null)
     { 
    <h3>Sprints</h3> 
    <center>
    <table class="table" style="width:700px; border: 1px solid #CFCACC;"> 
        <tr>
            <th>Date From</th> 
            <th>Date To</th> 
            <th>Velocity</th>
            @*<th>Status</th>*@
        </tr> 
 
        @foreach (var item in Model.Sprints)
        {
            string selectedRow = "";
            if (item.Id == ViewBag.StoriesID)
            {
                selectedRow = "selectedrow";
            } 
        
            <tr class="@selectedRow"> 
                <td><center>@item.DateFrom</center></td> 
                <td><center>@item.DateTo</center></td> 
                <td><center>@item.Velocity</center></td> 
                @*<td><center>@item.Status</center></td>*@
            </tr> 
        } 
    </table>
    </center>
    
        if (ViewBag.manageSprints)
        {
        <center>@Html.ActionLink("Manage Sprints", "Index", "Sprint", new { projectId = ViewBag.projectId }, null)</center>
        }
     }
     
         <script type="text/javascript">
             document.getElementById("stories").innerHTML = document.getElementById("unfinished-stories").innerHTML;
             </script>